<%@ page import="BaseClass.PetInfo" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: 郭宇轩
  Date: 2020/11/14
  Time: 15:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>购物车</title>
    <style type="text/css">
        @import url("../static/css/Header.css");
    </style>

</head>

<%@ include file="../MainPage/Header.jsp"%>

<form action="../AddOrderServlet" method="post">
<!-- 获取装载数据的PetInfo-->
<% List<PetInfo> PI = (List<PetInfo>) session.getAttribute("CartPetInfo"); %>
<table border="1">
    <tr>
        <th>宠物种类</th>
        <th>宠物名字</th>
        <th>宠物价格</th>
        <th>数量</th>
    </tr>

        <% for(PetInfo p : PI){%>
    <tr>
        <td><%= p.getPetClass()%></td>
        <td><%= p.getPetName()%></td>
        <td><%= p.getCost()%></td>
        <td><input type="text" id="num<%=p.getPid()%>" onchange="numChanged('<%=p.getPid()%>')" value="<%= p.getNum()%>" name="petNum"/></td>
        <td><a href="../RemoveCartServlet?pid=<%=p.getPid() %>" type="text" onclick="">删除</a></td>
    </tr>
        <% } %>
    <tr>
        <td colspan="5" align="center"><input style=" font-size:19px;color: orange;background-color:black; border:0px;" type="submit" value="打包成订单"/></td>
    </tr>

</table>

</form>

</body>
</html>

<script>
    function numChanged(i) {
        var username = <%=session.getAttribute("username")%>;
        var pid = i;
        var petnum = document.getElementById("num"+pid);
        var num = petnum.value;
        var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function (){
            if(xhr.readyState==4 && xhr.status==200){
                location.reload();
            }
        }
        xhr.open("POST","${pageContext.request.contextPath}/UpdateCartInfo?username="+username+"&num="+num+"&pid="+pid);
        xhr.send(null)
    }
</script>